var main = document.querySelector(".main");
// 信息发送
var send = async function () {
  var value = input.value.trim();
  if (value !== "") {
    var div = document.createElement("div");
    div.classList.add("chat-container", "avatar-container", "clearfix");
    div.innerHTML = `
        <img src="./img/avatar.webp" alt="">
            <div class="chat-txt">${value}</div>
        `;
    main.appendChild(div);
    input.value = "";
    main.scrollTop = main.scrollTop + 120;
    var authorization = sessionStorage.Authorization;
    var response = await fetch("https://study.duyiedu.com/api/chat", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        authorization: authorization,
      },
      body: JSON.stringify({
        content: value,
      }),
    });
    var res = (await response.json()).data.content;
    var div1 = document.createElement("div");
    div1.classList.add("chat-container", "robot-container", "clearfix");
    div1.innerHTML = `
        <img src="https://img2.baidu.com/it/u=1006592210,3539228340&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=500&amp;h=500"
                alt="">
            <div class="chat-txt">${res}</div>`;
    main.appendChild(div1);
    main.scrollTop = main.scrollTop + 60;
  }
};

input.onkeydown = function (e) {
  if (e.code === "Enter") {
    send();
  }
};
sendBtn.onclick = function () {
  send();
};
